package tuyou.kafka.consumer;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import tuyou.kafka.producer.ProducerTest;

/**
 * 消息消费者
 * 
 * @author：涂有
 * @date 2017年6月1日 上午11:27:10
 */
public class ConsumerTest {
	private String group = "group1";
	private final int timeSection = 100;
	private boolean autoCommit = true;
	private List<String> topics = Arrays.asList(ProducerTest.topic);

	public void consumer() {
		Properties properties = new Properties();
		properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getCanonicalName());// key反序列化方式
		properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getCanonicalName());// value反系列化方式
		properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, autoCommit);// 提交方式
		properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, ProducerTest.ip);// 指定broker地址，来找到group的coordinator
		properties.put(ConsumerConfig.GROUP_ID_CONFIG, group);// 指定用户组

		KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);

		//指定offset消费
//		TopicPartition p = new TopicPartition(topics.get(0), 0);
//		consumer.assign(Arrays.asList(p));
//		consumer.seekToBeginning(Arrays.asList(p));
		
		consumer.subscribe(topics);// 指定topic消费
		
		while (true) {
			System.out.println("开始poll---------------");
			ConsumerRecords<String, String> records = consumer.poll(timeSection);// 拉取一次数据
			for (ConsumerRecord<String, String> record : records) {
				System.out.println("topic: " + record.topic() + " key: " + record.key() + " value: " + record.value()
						+ " partition: " + record.partition());
			}
			if (!autoCommit) {
				consumer.commitAsync();// 手动commit
			}
//			System.out.println("完成一次poll---------------");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		new ConsumerTest().consumer();
	}
}